(function($) {
	var list = {};
	$.oCrop = function(id, config) {
	            if(!id || !list[id]) {
	                var D = new oCrop(config);
	                id = !id ? D.elem[0].id : id;
	                list[id] = D;
	            }
	            return list[id];
	        };
	$.oFCrop = function(id) {
	            if(list[id]) {
		            return list[id];
	            }
	            return null;
	        };
	
	
	var _id_counter = 0,

	EVENT_LIST = {
		"CENTER" : "center",
		"BEFORESHOW" : "beforeShow",
		"SHOW" : "show",
		"BEFOREHIDE" : "beforeHide",
		"HIDE" : "hide"
	};

	function oCrop(config) {
		var self = this, cfg = self.config;
		cfg = mix(
				{ 
					id : null,
					crop_sid : null,
					crop_cid : null,
					crop_type : 'home',
					crop_url : url_crophotshare,
					crop_imgpath : null,
					crop_imgpath_rel : null,
					crop_position : null,
					crop_style : null,
					crop_width : null,
					crop_height : null,
					crop_uid : null,
					aspectRatio : null,
					className : 'ui-dialog',
					tpl : '<div id="%ID" class="%CLASSNAME"><div class="hd"><button type="button" class="close">×</button><h2>Loading</h2></div><div class="bd"><div class="ui-loading">Loading...</div></div><div class="ft"></div</div>'
				}, config || {});
		cfg.id = cfg.id || 'ui-crop-' + counter();
		self.config = cfg;
	}

	mix(
			oCrop.prototype,
			{

				_crop_image : null,
				
				_status : 400,

				findPushShare : function(sid,cid,path) {
					var self = this;
					var tpl="push_dialog";
					getTpl(tpl,function(tplexisted){
						self.config.crop_cid = cid;
						self.config.crop_sid = sid;
						self.config.crop_type = 'home';
						self.config.crop_imgpath = path||self.config.crop_imgpath;
						self.config.crop_imgpath_rel = path||self.config.crop_imgpath_rel;
						var data = {'cid':self.config.crop_cid,'sid':self.config.crop_sid};
						ajaxpost(url_categoryhotshare, data, 
							function(result){
								var tpl_ins = $('#'+tpl+'_tpl');
								var body = $(tpl_ins.html()).find('div.pbody');
								var pfunction = $(tpl_ins.html()).find('div.pfunction');
								var pfunctiontxt =pfunction.text();
								result.imagepath = self.config.crop_imgpath;
								var lis = $(Mustache.to_html(body.html(), result));
								var width = body.attr('data-width');
								var title = body.attr('data-title');
								var css = body.attr('data-css-class');
								var d=getStaticDialog().width(width).head(title).body(lis);
								d.elem.addClass(css);
								d.show();
								self.switchPushStyle(result.data.style);
								if(!tplexisted&&pfunctiontxt){
									var pfunctiontxt = Mustache.to_html(pfunctiontxt, result);
									//console.log(pfunctiontxt);
									eval(pfunctiontxt);
								}
							},
							function(result){
								showError(result.message);
							});
						 
					 });
				},
				
				starOpen : function(uid,cid,sid){
					var data= {'uid': uid,'sid': sid,'cid': cid};
					self.config.crop_sid = sid;
					self.config.crop_type = 'star';
					self.config.crop_uid = uid;
					self.config.crop_cid = cid;
					self.config.crop_url = url_starcovercrop;
					ajaxpost(url_getstaruser, data, 
							function(result){
								var tpl="star_open";
								getTpl(tpl,function(){
									var tpl_ins = $('#'+tpl+'_tpl');
									var body = $(tpl_ins.html()).find('div.pbody');
									var lis = $(Mustache.to_html(body.html(), result));
									var width = body.attr('data-width');
									var title = body.attr('data-title');
									var css = body.attr('data-css-class');
									var d=getStaticDialog().width(width).head(title).body(lis);
									d.elem.addClass(css);
									d.show();
									if(result.data.star_cover!=null&&result.data.star_cover!=undefined&&result.data.star_cover!=''){
										self.switchPushStyle(result.data.star_cover.style);
									}
									self.config.crop_imgpath = result.data.image_path;
								 });
							},
							function(result){
								var tpl="star_open_confirm";
								getTpl(tpl,function(){
									var tpl_ins = $('#'+tpl+'_tpl');
									var body = $(tpl_ins.html()).find('div.pbody');
									var lis = $(Mustache.to_html(body.html(), result.data));
									var width = body.attr('data-width');
									var title = body.attr('data-title');
									var css = body.attr('data-css-class');
									var d=getStaticDialog().width(width).head(title).body(lis);
									d.elem.addClass(css);
									d.show();
								 });
							});
				},

				
				switchPushStyle : function(type) {
					var self = this;
					self.config.crop_style = type;
					$('#push_'+type).parent().find('.gallery').addClass("hide");
					$('#push_'+type).parent().parent().find('.active').removeClass("active");
					$('#push_'+type).removeClass("hide");
					$('#link_'+type).addClass("active");
				},

				openCrop : function(position, aspectRatio) {
					var self = this;
					self.config.crop_position = position;
					self.config.aspectRatio = aspectRatio;
					self.openPushCropDialog(aspectRatio);
				},

				openPushCropDialog : function(aspectRatio) {
					var self = this;
					var ass = aspectRatio.split(':');
					self.config.crop_width=ass[0];
					self.config.crop_height=ass[1];
					
					var tpl="crop_dialog";
					var data = {
							'path': self.config.crop_imgpath,
							'sid': self.config.crop_sid
						};
					getTpl(tpl,function(){
						var tpl_ins = $('#'+tpl+'_tpl');
						var body = $(tpl_ins.html()).find('div.pbody');
						var lis = $(Mustache.to_html(body.html(), data));
						var width = body.attr('data-width');
						var title = body.attr('data-title');
						var css = body.attr('data-css-class');
						var d=getStaticDialog().width(width).head(title).body(lis);
						d.elem.addClass(css);
						d.show();
						
						$('#ui_puzz_dialog').imagesLoaded(function() {
							if (self._crop_image != null && self._crop_image != undefined) {
								self._crop_image.remove();
							}
							setTimeout(function() {
								self._crop_image = $('#crop_image_' + self.config.crop_sid).imgAreaSelect({
									zIndex: 2000,
									instance: true,
									aspectRatio: aspectRatio,
									show: true,
									x1: 5,
									y1: 5,
									x2: 100,
									y2: 100,
									handles: true
								});
							}, 500);

							$(d).bind("hide",function() {
								if (!isUndefined(self._crop_image)&&self._crop_image!=null) self._crop_image.remove();
							});
						});
					 });
				},

				crop : function() {
					var self = this;
					var selection = self._crop_image.getSelection();
					var w = selection.x2 - selection.x1;
					var h = selection.y2 - selection.y1;
					var js_h = $('#crop_image_' + self.config.crop_sid).height();
					var js_w = $('#crop_image_' + self.config.crop_sid).width();
					self.pushCrop(self.config.crop_sid, self.config.crop_cid, self.config.crop_position, selection.x1, selection.y1, w, h, js_w, js_h);
					return self;
				},
				
				pushCrop : function(sid, cid, position, x, y, w, h, js_w, js_h) {
					var self = this;
					var crop_dialog = $('#crop_dialog_tpl');
					var ww = self.config.crop_width;
					var hh = self.config.crop_height;
					var type = self.config.crop_type;
					var style = self.config.crop_style;
					var uid = self.config.crop_uid;
					var data = {'sid':sid,'rid':cid,'cid':cid,'uid':uid,'position':position,'x':x,'y':y,'w':w,'h':h,'js_w':js_w,'js_h':js_h,'ww':ww,'hh':hh,'sy':style};
					ajaxpost(self.config.crop_url, data, 
							function(result){
								if (!isUndefined(self._crop_image)&&self._crop_image!=null) self._crop_image.remove();
								if(type=='home'){
									self.findPushShare(sid, cid);
								}else if(type=='star'){
									self.starOpen(uid,cid,sid);
								}
							},
							function(result){
								showError(result.message);
							});
				},
				
				saveStaruser : function(cid, uid, star_reason){
					var self = this;
					var data= {'sreason': star_reason,'uid': uid,'rid': cid};
					ajaxpost(url_createstaruser, data, 
							function(result){
								self.starOpen(uid, cid, self.config.crop_sid);
							},
							function(result){
								showError(result.message);
							});
				},


				starOpen : function(uid,cid,sid) {
					var self = this;
					var data= {'uid': uid,'sid': sid,'cid': cid};
					self.config.crop_sid = sid;
					self.config.crop_type = 'star';
					self.config.crop_uid = uid;
					self.config.crop_cid = cid;
					self.config.crop_url = url_starcovercrop;
					ajaxpost(url_getstaruser, data, 
							function(result){
								var tpl="star_open";
								getTpl(tpl,function(){
									var tpl_ins = $('#'+tpl+'_tpl');
									var body = $(tpl_ins.html()).find('div.pbody');
									var lis = $(Mustache.to_html(body.html(), result));
									var width = body.attr('data-width');
									var title = body.attr('data-title');
									var css = body.attr('data-css-class');
									var d=getStaticDialog().width(width).head(title).body(lis);
									d.elem.addClass(css);
									d.show();
									if(result.data.star_cover!=null&&result.data.star_cover!=undefined&&result.data.star_cover!=''){
										self.switchPushStyle(result.data.star_cover.style);
									}
									self.config.crop_imgpath = result.data.image_path;
								 });
							},
							function(result){
								var tpl="star_open_confirm";
								getTpl(tpl,function(){
									var tpl_ins = $('#'+tpl+'_tpl');
									var body = $(tpl_ins.html()).find('div.pbody');
									var lis = $(Mustache.to_html(body.html(), result.data));
									var width = body.attr('data-width');
									var title = body.attr('data-title');
									var css = body.attr('data-css-class');
									var d=getStaticDialog().width(width).head(title).body(lis);
									d.elem.addClass(css);
									d.show();
								 });
							});
				}
				
				
			});

	function mix(r, s) {
		for ( var k in s) {
			r[k] = s[k];
		}
		return r;
	}

	function counter() {
		return _id_counter++;
	}

})(jQuery);